package com.wdz.mvr.controllers;

import com.wdz.mvr.Services.ClassificationService;
import com.wdz.mvr.entities.Classification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("classifications")
public class ClassificationController {

    @Autowired
    ClassificationService classificationService;

    @GetMapping
    public String displayClassifications(Model model) {

        List<Classification> classifications = classificationService.getAll();
        model.addAttribute("classifications", classifications);
        return "classifications/list-classifications";
    }

    @GetMapping("/new")
    public String displayClassificationForm(Model model) {

        Classification aClassification = new Classification();
        model.addAttribute("classification", aClassification);
        return "classifications/new-classification";
    }

    @PostMapping("/save")
    public String createStudent(Model model,  Classification classification) {
        //使用student crudRepository保存到数据库
        classificationService.save(classification);


        return "redirect:/classifications";
    }
}

